import {
  ETCList,
  rechange,
  etcCardAdd,
  etcCardEdit,
  etcDetails,
  deleteEtc,
  disabledTmsEtcCard,
  enableTmsEtcCard,
  etcChangeList,
  etcChangeAdd,
  etcChangeEdit,
  etcChangetDetails,
  importEtcChange,
  generatedBills,
  selectAll,
} from '@/services/manageEtc';
import { memberExpression } from '@babel/types';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'manageEtc',

  state: {
    ETCList: {},
    etcChangeList: {},
    etcDetails: {},
    etcChangetDetails: {},
  },

  effects: {
    *selectAll({ payload, callback }, { call }) {
      const response = yield call(selectAll, payload);
      if (response.code === 0) {
        Prompt({ content: `账单生成${response.message}!` });
        if (callback) callback(response.data);
      } else {
        if (callback) callback();
      }
    },
    //ETC卡管理：
    *ETCList({ payload, callback }, { call, put }) {
      const response = yield call(ETCList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'show',
          payload: {
            ETCList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *EtcOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? etcCardEdit : etcCardAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *etcDetails({ payload, callback }, { call, put }) {
      const response = yield call(etcDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detailETC',
          payload: {
            etcDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *disabledTmsEtcCard({ payload, callback }, { call, put }) {
      const response = yield call(disabledTmsEtcCard, payload);
      if (response.code === 0) {
        callback(response);
      }
    },
    *enableTmsEtcCard({ payload, callback }, { call, put }) {
      const response = yield call(enableTmsEtcCard, payload);
      if (response.code === 0) {
        callback(response);
      }
    },
    *rechange({ payload, callback }, { call, put }) {
      const response = yield call(rechange, payload);
      if (response.code === 0) {
        Prompt({ content: '充值成功' });
        callback(response);
      }
    },
    *deleteEtc({ payload, callback }, { call, put }) {
      const response = yield call(deleteEtc, payload);
      if (response.code === 0) {
        Prompt({ content: `删除${response.message}` });
        callback(response);
      }
    },

    //ETC交易管理
    *etcChangeList({ payload, callback }, { call, put }) {
      const response = yield call(etcChangeList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'showChange',
          payload: {
            etcChangeList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *EtcChangeOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? etcChangeEdit : etcChangeAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *etcChangetDetails({ payload, callback }, { call, put }) {
      const response = yield call(etcChangetDetails, payload);

      if (response.code === 0) {
        yield put({
          type: 'detailChange',
          payload: {
            etcChangetDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *importEtcChange({ payload, callback }, { call }) {
      const response = yield call(importEtcChange, payload);
      if (response.code === 0) {
        Prompt({ content: response.message });
        if (callback) callback(response.data);
      }
    },
    /**
     * 生成账单
     */
    *generatedBills({ payload, callback }, { call }) {
      const response = yield call(generatedBills, payload);
      const { code, message, data } = response;
      if (code === 0) {
        Prompt({ content: `账单生成${message}!` });
        if (callback) callback(data);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ETCList: payload.ETCList,
      };
    },
    showChange(state, { payload }) {
      return {
        ...state,
        etcChangeList: payload.etcChangeList,
      };
    },
    detailChange(state, { payload }) {
      return {
        ...state,
        etcChangetDetails: { ...state.etcChangetDetails, ...payload.etcChangetDetails },
      };
    },
    detailETC(state, { payload }) {
      return {
        ...state,
        etcDetails: { ...state.etcDetails, ...payload.etcDetails },
      };
    },
  },
};
